import java.util.Objects;
import java.util.Stack;

public class LeetCode150 {
    public static void main(String[] args) {
        String[] arr = {"2","1","+","3","*"};
        int i = evalRPN(arr);
        System.out.println(i);
    }

    public static int evalRPN(String[] tokens) {
        /*
        思路：遇到数字则压入栈，遇到运算符号则弹出两个进行计算，把计算的结果压入栈，
         */
        Stack<String> stack = new Stack<>();
        for (String token : tokens) {
            if (Objects.equals(token, "+")){
                String pop1 = stack.pop();
                String pop2 = stack.pop();
                stack.push(String.valueOf(Integer.parseInt(pop2) + Integer.parseInt(pop1)));
            }else if (Objects.equals(token, "-")){
                String pop1 = stack.pop();
                String pop2 = stack.pop();
                stack.push(String.valueOf(Integer.parseInt(pop2) - Integer.parseInt(pop1)));
            }else if (Objects.equals(token, "*")){
                String pop1 = stack.pop();
                String pop2 = stack.pop();
                stack.push(String.valueOf(Integer.parseInt(pop2) * Integer.parseInt(pop1)));
            }else if (Objects.equals(token, "/")){
                String pop1 = stack.pop();
                String pop2 = stack.pop();
                stack.push(String.valueOf(Integer.parseInt(pop2) / Integer.parseInt(pop1)));
            }else {
                stack.push(token);
            }

        }
        return Integer.parseInt(stack.pop());
    }
}

